package com.li.kmp;

import java.util.Arrays;

public class kmp {
    public static void main(String[] args) {
        String str1 = "BBC ABCDAB ABCDABCDABDE";
        String str2 = "ABCDABD";
        int[] ints = kmpnext(str2);
        System.out.println(Arrays.toString(ints));
    }
    public static int[] kmpnext(String dest){
        int[] next=new int[dest.length()];
        next[0]=0;
        for (int i = 1,j=0; i <dest.length() ; i++) {
            while (j > 0 && dest.charAt(i) != dest.charAt(j)) {
                j = next[j - 1];
            }
            if (dest.charAt(i)==dest.charAt(j)){
                j++;
            }
            next[i]=j;

        }
        return next;
    }
}
